package com.bdqn.gulimall.cart.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置类
 *
 **/

@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new ThreadPoolExecutor(
                // 核心线程数
                pool.getCoreSize(),
                // 最大线程数
                pool.getMaxSize(),
                // 线程存活时间
                pool.getKeepAliveTime(),
                // 线程过期时间
                TimeUnit.SECONDS,
                // 方法的容量是Interger类型的最大值 this(Integer.MAX_VALUE); 内存不够
                new LinkedBlockingDeque<>(100000),
                // 默认的线程工厂
                Executors.defaultThreadFactory(),
                //AbortPolicy()方法 直接丢弃新来的任务
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

}
